/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   NewTechnologyServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz.newTechnology    </p>
 * 
 * <p>最后修改 : $: 2017年8月10日-下午7:27:57 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.newTechnology;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jsbz.newTechnology.NewTechnologyDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.MaintenTechnology;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.newTechnology.NewTechnologyService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * NewTechnologyServiceImpl
 * 
 * 2017年8月10日 下午7:27:57
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class NewTechnologyServiceImpl implements NewTechnologyService  {
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	public NewTechnologyDao newTechnologyDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	public List<MaintenTechnology> getNewTechnologyList(QueryFilter queryFilter){
		return newTechnologyDao.find(queryFilter,"winningTime", true);
	}
	/**
	 * 查询，根据查询条件获取结果
	 */
	@Override
	public Map<String, Object> getMaintenTechnology(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<MaintenTechnology> maintenTechnologyList = new ArrayList<MaintenTechnology>();
		List<Attachment> list = new ArrayList<Attachment>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double reserveMark =  Double.parseDouble((String)jsonObject.get("reserveMark"));
		double firstStepScore =  Double.parseDouble((String)jsonObject.get("firstStepScore"));
		
		//取到所有文件
		list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//取得获奖文件
		List<Attachment> kjhjist = systemManageService.getCheckAttachmentByTag(startTime, endTime, "yhjs_kjhj");
		//取得技术总结、指南文件
		List<Attachment> jsznList = systemManageService.getAllCheckAttachments("yhjs_jszn");
		//查询数据
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("winningTime", startTime, MatchType.GE);
		queryFilter.addFilter("winningTime", endTime, MatchType.LE);
		//检查记录
		double total = 0.0;				//得分
		double percentage = 0.0;		//完善度
		double temp = 0;				//获奖总分
		double attMark =0;				//技术类文件得分
		double tableFull = MathsUtils.sumDouble(firstStepScore, yearPart);
		tableFull = MathsUtils.formatDouble(tableFull, 2);         //满分
		
		maintenTechnologyList = this.getNewTechnologyList(queryFilter);
		for (int i = 0; i < maintenTechnologyList.size(); i++) {
			MaintenTechnology maintenTechnology = maintenTechnologyList.get(i);
			if (maintenTechnology.getAttachments().size() > 0) {
				for ( int j = 0 ;j >= maintenTechnology.getAttachments().size(); j++){
					if ("yhjs_kjhj".equals(maintenTechnology.getAttachments() .get(j).getTag())) {
						if (maintenTechnology.getWinningGrade().equals("1")) {   //1 表示国家级
							total = MathsUtils.sumDouble(total, reserveMark);
							temp = MathsUtils.sumDouble(temp, reserveMark);
						} else {
							total = MathsUtils.sumDouble(total, poitMark);
							temp = MathsUtils.sumDouble(temp, poitMark);
						}
						break;
					}
				}
				if (total >= firstStepScore) {
					total = firstStepScore;
					temp = firstStepScore;
					break;
				}
			}
		}
		if (jsznList.size() <= 5 && jsznList.size() > 0) {
			total = MathsUtils.sumDouble(total, yearFull);
			attMark = MathsUtils.sumDouble(attMark, yearFull);
		} else if (jsznList.size() >= 5) {
			total = MathsUtils.sumDouble(total, yearPart);
			attMark = MathsUtils.sumDouble(attMark, yearPart);
		}
		
		map.put("sum", total);
		//判断完善度
		percentage = MathsUtils.divDouble(total, tableFull, 2);
		percentage = MathsUtils.mulDouble(percentage, 100);
		map.put("percent", percentage);
		
		map.put("attMark", attMark);
		map.put("temp", temp);
		map.put("att", list);
		map.put("kjhjist", kjhjist);
		map.put("jsznList", jsznList);
		map.put("checkTable", checkTable);
		map.put("changeInves", maintenTechnologyList);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("score", checkTable.getTestFull());
		return map;
	}
}
